
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.34 Pod 端口映射 · Kubernetes - 痴者工良</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="痴者工良">
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="5.update.html" />
    
    
    <link rel="prev" href="3.replica.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://www.whuanle.cn" target="_blank" class="custom-link">痴者工良的博客</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    文档说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../1.basic/">
            
                <a href="../1.basic/">
            
                    
                    1.基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../1.basic/">
            
                <a href="../1.basic/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../1.basic/1.docker.html">
            
                <a href="../1.basic/1.docker.html">
            
                    
                    1.1 说透 Docker：基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../1.basic/2.virtual.html">
            
                <a href="../1.basic/2.virtual.html">
            
                    
                    1.2 说透 Docker： 虚拟化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../1.basic/3.docker_network.html">
            
                <a href="../1.basic/3.docker_network.html">
            
                    
                    1.3 了解 Docker 网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../1.basic/4.pod_docker.html">
            
                <a href="../1.basic/4.pod_docker.html">
            
                    
                    1.4 Docker 和 Pod
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../1.basic/5.k8s.html">
            
                <a href="../1.basic/5.k8s.html">
            
                    
                    1.5 K8S入门基础
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../2.deploy/">
            
                <a href="../2.deploy/">
            
                    
                    2.部署和配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../2.deploy/2.deploy.md">
            
                <span>
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../2.deploy/1.minikube.html">
            
                <a href="../2.deploy/1.minikube.html">
            
                    
                    2.1 使用 Minikube 部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../2.deploy/2.kubeadm.html">
            
                <a href="../2.deploy/2.kubeadm.html">
            
                    
                    2.2 使用 kubeadm 部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../2.deploy/3.kubeadm_ckad.html">
            
                <a href="../2.deploy/3.kubeadm_ckad.html">
            
                    
                    2.3 CKAD认证中的部署教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../2.deploy/4.kubeadm_proxy.html">
            
                <a href="../2.deploy/4.kubeadm_proxy.html">
            
                    
                    2.4 国内代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../2.deploy/5.dashboard.html">
            
                <a href="../2.deploy/5.dashboard.html">
            
                    
                    2.5 Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    3.Pod部署和调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="./">
            
                <a href="./">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="1.pod.html">
            
                <a href="1.pod.html">
            
                    
                    3.1 Pod
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="2.deployment.html">
            
                <a href="2.deployment.html">
            
                    
                    3.2 Deployment部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="3.replica.html">
            
                <a href="3.replica.html">
            
                    
                    3.3 副本集(ReplicaSet)
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.5" data-path="4.pod_network.html">
            
                <a href="4.pod_network.html">
            
                    
                    3.34 Pod 端口映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="5.update.html">
            
                <a href="5.update.html">
            
                    
                    3.5 Pod 升级、回滚
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="6.scale.html">
            
                <a href="6.scale.html">
            
                    
                    3.6 Pod 缩放
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="7.lable.html">
            
                <a href="7.lable.html">
            
                    
                    3.7.Pod 标签
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="8.schedule.html">
            
                <a href="8.schedule.html">
            
                    
                    3.8 Pod 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="9.jobs_cronjobs.html">
            
                <a href="9.jobs_cronjobs.html">
            
                    
                    3.9 Jobs、CronJobs
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../4.network/">
            
                <a href="../4.network/">
            
                    
                    4.Kubernetes 网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../4.network/">
            
                <a href="../4.network/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../4.network/1.network.html">
            
                <a href="../4.network/1.network.html">
            
                    
                    4.1 Kubernetes 网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../4.network/2.endpoint.html">
            
                <a href="../4.network/2.endpoint.html">
            
                    
                    4.2 Endpoint
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../4.network/3.ingress.html">
            
                <a href="../4.network/3.ingress.html">
            
                    
                    4.3 ingress
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../4.network/4.discovery.html">
            
                <a href="../4.network/4.discovery.html">
            
                    
                    4.4 服务发现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../5.volumes/">
            
                <a href="../5.volumes/">
            
                    
                    5.volumes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../5.volumes/">
            
                <a href="../5.volumes/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../5.volumes/1.volumes.html">
            
                <a href="../5.volumes/1.volumes.html">
            
                    
                    5.1 卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../5.volumes/2.secret_configmap.html">
            
                <a href="../5.volumes/2.secret_configmap.html">
            
                    
                    5.2 secret 和 ConfigMap 卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../5.volumes/3.nfts.html">
            
                <a href="../5.volumes/3.nfts.html">
            
                    
                    5.3 NFS卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../5.volumes/4.pv_pvc.html">
            
                <a href="../5.volumes/4.pv_pvc.html">
            
                    
                    5.4 持久化卷
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../6.api_safe/">
            
                <a href="../6.api_safe/">
            
                    
                    6.API 和 安全
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../6.api_safe/">
            
                <a href="../6.api_safe/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../7.health/">
            
                <a href="../7.health/">
            
                    
                    7.维护和故障排除
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../8.health/README.md">
            
                <span>
            
                    
                    导读
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../k8s.html">
            
                <a href="../k8s.html">
            
                    
                    Kubernetes 命令概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../ckad.html">
            
                <a href="../ckad.html">
            
                    
                    CKAD 认证帮助
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.34 Pod 端口映射</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#34-pod-&#x7AEF;&#x53E3;&#x6620;&#x5C04;"><b></b>3.4 Pod &#x7AEF;&#x53E3;&#x6620;&#x5C04;</a></li><ul><ul><li><span class="title-icon "></span><a href="#containerport"><b></b>containerPort</a></li></ul><li><span class="title-icon "></span><a href="#&#x7F51;&#x7EDC;&#x7AEF;&#x53E3;&#x6620;&#x5C04;"><b></b>&#x7F51;&#x7EDC;&#x7AEF;&#x53E3;&#x6620;&#x5C04;</a></li><li><span class="title-icon "></span><a href="#&#x672C;&#x5730;&#x7AEF;&#x53E3;"><b></b>&#x672C;&#x5730;&#x7AEF;&#x53E3;</a></li></ul></ul></div><h1 id="34-pod-&#x7AEF;&#x53E3;&#x6620;&#x5C04;"><a name="34-pod-&#x7AEF;&#x53E3;&#x6620;&#x5C04;" class="anchor-navigation-ex-anchor" href="#34-pod-&#x7AEF;&#x53E3;&#x6620;&#x5C04;"><i class="fa fa-link" aria-hidden="true"></i></a>3.4 Pod &#x7AEF;&#x53E3;&#x6620;&#x5C04;</h1>
<p>&#x5728; 3.1&#xFF0C;3.2 &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x90E8;&#x7F72;&#x8FC7;&#x4E86; Nginx &#x5BB9;&#x5668;&#xFF0C;&#x4F7F;&#x7528;&#x4E86; <code>--port=8080</code> &#x6216; <code>containerPort: 8080</code> &#x4E3A; Pod &#x66B4;&#x9732;&#x4E00;&#x4E2A;&#x7AEF;&#x53E3;&#xFF0C;&#x672C;&#x7AE0;&#x53EA;&#x662F;&#x7B80;&#x5355;&#x5730;&#x4E3A; Pod &#x521B;&#x5EFA; Service&#xFF0C;&#x5E76;&#x4E14;&#x4ECB;&#x7ECD; Pod &#x7684;&#x4E00;&#x4E9B;&#x7F51;&#x7EDC;&#x77E5;&#x8BC6;&#xFF0C;&#x5728;&#x7B2C;&#x56DB;&#x7AE0;&#x4E2D;&#x4F1A;&#x8BE6;&#x7EC6;&#x8BB2;&#x89E3;&#x7F51;&#x7EDC;&#x65B9;&#x9762;&#x7684;&#x77E5;&#x8BC6;&#x3002;</p>
<h3 id="containerport"><a name="containerport" class="anchor-navigation-ex-anchor" href="#containerport"><i class="fa fa-link" aria-hidden="true"></i></a>containerPort</h3>
<p>&#x8FD9;&#x4E2A;&#x5B57;&#x6BB5;&#x7528;&#x4E8E;&#x89C4;&#x8303;&#x5316;&#x58F0;&#x660E;&#x5BB9;&#x5668;&#x5BF9;&#x5916;&#x66B4;&#x9732;&#x7684;&#x7AEF;&#x53E3;&#xFF0C;&#x4F46;&#x8FD9;&#x4E2A;&#x7AEF;&#x53E3;&#x5E76;&#x4E0D;&#x662F;&#x5BB9;&#x5668;&#x6620;&#x5C04;&#x5230;&#x4E3B;&#x673A;&#x7684;&#x7AEF;&#x53E3;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x58F0;&#x660E;&#x5F0F;&#x7684;&#x5B57;&#x6BB5;&#xFF0C;&#x5C5E;&#x4E8E;&#x5BB9;&#x5668;&#x7AEF;&#x53E3;&#x89C4;&#x8303;&#x3002;</p>
<p>&#x5728;&#x5F88;&#x591A;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x6B64; containerPort &#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE; Pod&#x3002;</p>
<p>&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x628A;&#x4E0A;&#x4E00;&#x7AE0;&#x4E2D;&#x521B;&#x5EFA;&#x7684; deployment &#x5220;&#x9664;&#xFF0C;&#x7136;&#x540E;&#x91CD;&#x65B0;&#x521B;&#x5EFA;&#x3002;</p>
<pre class="language-"><code class="lang-bash">kubectl create deployment nginx --image<span class="token operator">=</span>nginx:latest
</code></pre>
<p>&#x7136;&#x540E;&#x83B7;&#x53D6; Pod &#x7684; IP&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl get pods -o wide</span>
NAME                     READY   STATUS    RESTARTS   AGE     IP          NODE     NOMINATED NODE   READINESS GATES
nginx-55649fd747-zndzx   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          7m57s   <span class="token number">10.32</span>.0.2   slave1   <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>           <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>
</code></pre>
<p>&#x7136;&#x540E;&#x8BBF;&#x95EE; 10.32.0.2 &#xFF0C;&#x4F1A;&#x53D1;&#x73B0;&#x6B63;&#x5E38;&#x6253;&#x5F00;&#x3002;</p>
<p><img src="images/images/.jpg" alt="./images/"></p>
<p>&#x52A0;&#x4E0D;&#x52A0;&#x7AEF;&#x53E3;&#x6620;&#x5C04;&#x90FD;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE; 80 &#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x5BB9;&#x5668;&#x7AEF;&#x53E3;&#x89C4;&#x8303;&#xFF0C;&#x8DDF; Dockerfile &#x7684; port &#x4E00;&#x6837;&#xFF0C;&#x4E0D;&#x5177;&#x6709;&#x5F3A;&#x5236;&#x5F00;&#x653E;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5728;&#x521B;&#x5EFA; Service &#x65F6;&#x6709;&#x7528;&#x5904;&#x3002;</p>
<blockquote>
<p><strong>[Error] &#x63D0;&#x793A;</strong></p>
<p>&#x5982;&#x679C;&#x4F60;&#x6CA1;&#x6709;&#x914D;&#x7F6E;&#x8FC7; CNI &#x7F51;&#x7EDC;&#x63D2;&#x4EF6;&#xFF0C;&#x5219;&#x521B;&#x5EFA;&#x7684; Pod IP&#xFF0C;&#x53EA;&#x80FD;&#x5728;&#x6240;&#x5728;&#x7684;&#x8282;&#x70B9;&#x4E2D;&#x8BBF;&#x95EE;&#xFF0C;&#x8981;&#x8DE8;&#x8282;&#x70B9;&#x8BBF;&#x95EE;&#xFF0C;&#x8BF7;&#x6309;&#x7167; 2.2 &#x7684;&#x521D;&#x59CB;&#x5316;&#x7F51;&#x7EDC;&#xFF0C;&#x6216; 2.3 &#x4E2D;&#x7684; Calico &#x4E00;&#x8282;&#xFF0C;&#x5B89;&#x88C5;&#x597D;&#x7F51;&#x7EDC;&#x63D2;&#x4EF6;&#x3002;</p>
<p>&#x53E6;&#x5916;&#xFF0C;&#x5982;&#x679C; Calico &#x914D;&#x7F6E;&#x9519;&#x8BEF;&#xFF0C;&#x4F1A;&#x5BFC;&#x81F4;&#x4E00;&#x76F4;&#x65E0;&#x6CD5;&#x521B;&#x5EFA; Pod&#x3002;</p>
</blockquote>
<p>&#x6211;&#x4EEC;&#x5728;&#x521B;&#x5EFA; Pod &#x65F6;&#xFF0C;&#x5982;&#x679C;&#x6307;&#x5B9A;&#x4E86; <code>--port</code> &#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x7AEF;&#x53E3;&#x4FBF;&#x4F1A;&#x751F;&#x6210; <code>containerPort</code> &#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x67E5;&#x770B;&#x521B;&#x5EFA;&#x7684; Deployment YAML &#x7684;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl create deployment nginx  --image<span class="token operator">=</span>nginx:latest --port<span class="token operator">=</span><span class="token number">80</span> --dry-run<span class="token operator">=</span>client -o yaml
</code></pre>
<pre class="language-"><code class="lang-yaml">      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>latest
        <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
        <span class="token key atrule">ports</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
        <span class="token key atrule">resources</span><span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre>
<h2 id="&#x7F51;&#x7EDC;&#x7AEF;&#x53E3;&#x6620;&#x5C04;"><a name="&#x7F51;&#x7EDC;&#x7AEF;&#x53E3;&#x6620;&#x5C04;" class="anchor-navigation-ex-anchor" href="#&#x7F51;&#x7EDC;&#x7AEF;&#x53E3;&#x6620;&#x5C04;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7F51;&#x7EDC;&#x7AEF;&#x53E3;&#x6620;&#x5C04;</h2>
<p>&#x5BF9;&#x4E8E; docker&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x6620;&#x5C04;&#x7AEF;&#x53E3;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>docker ... -p 6666:80</code>&#xFF0C;&#x90A3;&#x4E48;&#x5BF9;&#x4E8E; &#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x6216;&#x4F7F;&#x7528; Deployment &#x7B49;&#x65B9;&#x5F0F;&#x90E8;&#x7F72;&#x7684; Pod&#xFF0C;&#x90FD;&#x6709;&#x4E00;&#x4E2A; Pod IP &#x53EF;&#x4EE5;&#x5728;&#x96C6;&#x7FA4;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x8282;&#x70B9;&#x4E2D;&#x8BBF;&#x95EE;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E2A; IP &#x662F;&#x865A;&#x62DF; IP&#xFF0C;&#x4E0D;&#x80FD;&#x5728;&#x96C6;&#x7FA4;&#x5916;&#x4E2D;&#x8BBF;&#x95EE;&#xFF0C;&#x5373;&#x4F7F;&#x90FD;&#x662F;&#x5185;&#x7F51;&#x673A;&#x5668;&#xFF0C;&#x6CA1;&#x6709;&#x52A0;&#x5165; Kubernetes &#x96C6;&#x7FA4;&#xFF0C;&#x4E00;&#x6837;&#x4E0D;&#x80FD;&#x8BBF;&#x95EE;&#x3002;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x8981;&#x628A;&#x7AEF;&#x53E3;&#x66B4;&#x9732;&#x51FA;&#x53BB;&#xFF0C;&#x4F9B;&#x5916;&#x7F51;&#x8BBF;&#x95EE;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; Service&#x3002;</p>
<p>&#x5173;&#x4E8E; Service &#x7684;&#x77E5;&#x8BC6;&#xFF0C;&#x5728;&#x7B2C;&#x56DB;&#x7AE0;&#x4E2D;&#x4F1A;&#x8BE6;&#x7EC6;&#x8BB2;&#x89E3;&#xFF0C;&#x8FD9;&#x91CC;&#x4EC5;&#x8BF4;&#x660E;&#x5982;&#x4F55;&#x521B;&#x5EFA; Service&#xFF0C;&#x4EE5;&#x53CA; containerPort &#x7684;&#x4F5C;&#x7528;&#x3002;</p>
<p>&#x5728;&#x4E0A;&#x4E00;&#x5C0F;&#x8282;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x7684; Pod &#x6CA1;&#x6709;&#x58F0;&#x660E;&#x8FC7; containerPort&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x521B;&#x5EFA; Service &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x6620;&#x5C04;&#x7684;&#x7AEF;&#x53E3;&#x3002;</p>
<p>&#x67E5;&#x770B;&#x4E0A;&#x4E00;&#x8282;&#x521B;&#x5EFA;&#x7684; Deployment&#x3001;Pod&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl get deployments
kubectl get pods
</code></pre>
<pre class="language-"><code>NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           5m44s

NAME                     READY   STATUS    RESTARTS   AGE
nginx-55649fd747-9vfrx   1/1     Running   0          5m44s
</code></pre><p>&#x901A;&#x8FC7; <code>kubectl expose</code> &#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x521B;&#x5EFA; Service&#xFF0C;&#x5E76;&#x4E3A; Deployment &#x90E8;&#x7F72;&#x7684;&#x591A;&#x4E2A; Pod &#x66B4;&#x9732;&#x4E00;&#x4E2A;&#x76F8;&#x540C;&#x7684;&#x7AEF;&#x53E3;&#x3002;</p>
<p>&#x7531;&#x4E8E; Pod &#x4E2D;&#x7684; nginx &#x8BBF;&#x95EE;&#x7AEF;&#x53E3;&#x662F; 80&#xFF0C;&#x6211;&#x4EEC;&#x60F3;&#x5728;&#x5916;&#x7F51;&#x4E2D;&#x8BBF;&#x95EE;&#x65F6;&#x4F7F;&#x7528; 6666 &#x7AEF;&#x53E3;&#xFF0C;&#x5219;&#x547D;&#x4EE4;&#x5982;&#x4E0B;;</p>
<pre class="language-"><code class="lang-bash">kubectl expose deployment nginx --port<span class="token operator">=</span><span class="token number">6666</span> --target-port<span class="token operator">=</span><span class="token number">80</span>
<span class="token comment"># &#x6307;&#x5B9A;&#x6E90;&#x7AEF;&#x53E3;&#x4E3A; 80&#xFF0C;&#x8981;&#x6620;&#x5C04;&#x5230; 6666 &#x7AEF;&#x53E3;</span>
</code></pre>
<pre class="language-"><code class="lang-bash">kubectl expose deployment nginx --port<span class="token operator">=</span><span class="token number">80</span>
<span class="token comment"># &#x5982;&#x679C;&#x6CA1;&#x6709;&#x6307;&#x5B9A;&#x6E90;&#x7AEF;&#x53E3;&#xFF0C;&#x5219;&#x8868;&#x793A;&#x5176;&#x7AEF;&#x53E3;&#x4E5F;&#x662F; 80</span>
</code></pre>
<blockquote>
<p>&#x5982;&#x679C;&#x5BF9;&#x8C61;&#x76F4;&#x63A5;&#x662F; Pod &#x6216;&#x8005; DaemonSet &#x7B49;&#x5BF9;&#x8C61;&#xFF0C;&#x5219; <code>kubectl expose {&#x5BF9;&#x8C61;} ...</code>&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x4E3A;&#x6B64;&#x5BF9;&#x8C61;&#x4E2D;&#x7684; Pod &#x521B;&#x5EFA;&#x7AEF;&#x53E3;&#x6620;&#x5C04;&#x3002;</p>
</blockquote>
<p>&#x67E5;&#x770B; Service&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl get services
</code></pre>
<pre class="language-"><code>NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>none</span><span class="token punctuation">&gt;</span></span>        443/TCP    13d
nginx        ClusterIP   10.105.13.163   <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>none</span><span class="token punctuation">&gt;</span></span>        6666/TCP   6s
</code></pre><p>&#x7531;&#x4E8E;&#x6211;&#x4EEC;&#x53EA;&#x662F;&#x7B80;&#x5355;&#x5730;&#x521B;&#x5EFA; Service&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x6307;&#x5B9A; Service &#x7684;&#x516C;&#x5F00;&#x7A0B;&#x5EA6;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230; <code>TYPE</code> &#x4E2D;&#xFF0C;&#x5176;&#x7C7B;&#x578B;&#x4E3A; <code>ClusterIP</code>&#xFF0C;&#x8868;&#x793A;&#x53EA;&#x80FD;&#x5728; Pod &#x6240;&#x5728;&#x7684;&#x8282;&#x70B9;&#x4E0A;&#x901A;&#x8FC7;&#x6B64; IP &#x548C;&#x7AEF;&#x53E3;&#x8BBF;&#x95EE; Pod(&#x5982;&#x679C;&#x5B89;&#x88C5;&#x4E86; CNI &#x7F51;&#x7EDC;&#x63D2;&#x4EF6;&#xFF0C;&#x5219;&#x4EFB;&#x610F;&#x8282;&#x70B9;&#x7686;&#x53EF;&#x8BBF;&#x95EE;)&#x3002;</p>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>10.105.13.163</code> &#x548C; 6666 &#x7AEF;&#x53E3;&#x8BBF;&#x95EE; Pod &#x4E2D;&#x7684; Nginx &#x670D;&#x52A1;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x4ECB;&#x7ECD;&#x4E86;&#x5982;&#x4F55;&#x521B;&#x5EFA; Service&#xFF0C;&#x66B4;&#x9732;&#x7AEF;&#x53E3;&#xFF0C;&#x800C;&#x5728;&#x540E;&#x9762;&#x7684;&#x7AE0;&#x8282;&#x4E2D;&#xFF0C;&#x4F1A;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD; Service&#x3002;</p>
<blockquote>
<p><strong>[Info] &#x63D0;&#x793A;</strong></p>
<p>&#x5982;&#x679C;&#x5DF2;&#x7ECF;&#x6307;&#x5B9A;&#x8FC7; containerPort&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>kubectl expose deployment nginx</code> &#x5FEB;&#x901F;&#x6620;&#x5C04; containerPort &#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x7AEF;&#x53E3;&#x3002;</p>
</blockquote>
<h2 id="&#x672C;&#x5730;&#x7AEF;&#x53E3;"><a name="&#x672C;&#x5730;&#x7AEF;&#x53E3;" class="anchor-navigation-ex-anchor" href="#&#x672C;&#x5730;&#x7AEF;&#x53E3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x672C;&#x5730;&#x7AEF;&#x53E3;</h2>
<p>&#x6BCF;&#x4E2A; Pod &#x90FD;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x552F;&#x4E00; IP &#x5730;&#x5740;&#xFF0C;Pod &#x6240;&#x5728;&#x7684;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#xFF0C;&#x901A;&#x8FC7; IP &#x5730;&#x5740;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE; Pod&#x3002;</p>
<p>&#x9664;&#x4E86; Service&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>port-forward</code> &#x5728;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x76F4;&#x63A5;&#x6620;&#x5C04;&#x672C;&#x5730;&#x7AEF;&#x53E3;&#x5230; Pod&#x3002;</p>
<pre class="language-"><code>root@instance-2:~# kubectl port-forward nginx-55649fd747-s4824 666:80
Forwarding from 127.0.0.1:666 -&gt; 80
Forwarding from [::1]:666 -&gt; 80
</code></pre><p>&#x6B64;&#x65B9;&#x5F0F;&#x5177;&#x6709;&#x5F88;&#x5927;&#x9650;&#x5236;&#xFF0C;&#x9996;&#x5148;&#x5982;&#x679C; Pod &#x5728; <code>instance-2</code> &#x8282;&#x70B9;&#x4E2D;&#xFF0C;&#x90A3;&#x4E48;&#x6B64;&#x547D;&#x4EE4;&#x5728; <code>instance-2</code> &#x8282;&#x70B9;&#x4E0A;&#x8FD0;&#x884C;&#x624D;&#x80FD;&#x751F;&#x6548;&#xFF0C;&#x5728;&#x5176;&#x5B83;&#x8282;&#x70B9;&#x4E0A;&#x8FD0;&#x884C;&#x65E0;&#x6548;&#x3002;</p>
<p>&#x6B64;&#x65B9;&#x5F0F;&#x53EA;&#x80FD;&#x6620;&#x5C04;&#x672C;&#x5730;&#x7AEF;&#x53E3;&#xFF0C;&#x5982; <code>127.0.0.1</code>&#xFF0C;&#x4E0D;&#x80FD;&#x901A;&#x8FC7;&#x5916;&#x7F51;&#x8BBF;&#x95EE;&#x3002;</p>
<p>&#x539F;&#x672C;&#x53EA;&#x80FD;&#x901A;&#x8FC7; &#x5177;&#x4F53;&#x7684; Pod IP &#x624D;&#x80FD;&#x8BBF;&#x95EE; Pod&#xFF0C;&#x73B0;&#x5728;&#x5728;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x901A;&#x8FC7; 127.0.0.1 &#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x670D;&#x52A1;&#x3002;</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; &#x75F4;&#x8005;&#x5DE5;&#x826F; 2021 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x6700;&#x540E;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF1A;
2021-11-17 07:18:37
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="3.replica.html" class="navigation navigation-prev " aria-label="Previous page: 3.3 副本集(ReplicaSet)">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="5.update.html" class="navigation navigation-next " aria-label="Next page: 3.5 Pod 升级、回滚">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.34 Pod 端口映射","level":"1.4.5","depth":2,"next":{"title":"3.5 Pod 升级、回滚","level":"1.4.6","depth":2,"path":"3.pod/5.update.md","ref":"3.pod/5.update.md","articles":[]},"previous":{"title":"3.3 副本集(ReplicaSet)","level":"1.4.4","depth":2,"path":"3.pod/3.replica.md","ref":"3.pod/3.replica.md","articles":[]},"dir":"ltr"},"config":{"plugins":["tbfed-pagefooter","anchor-navigation-ex","mermaid-gb3","insert-logo","chapter-fold","advanced-emoji","github","splitter","alerts","popup","prism","-highlight","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy 痴者工良 2021","modify_label":"文档最后更新时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"prism":{"lang":{"flow":"typescript","shell":"bash"},"ignore":["mermaid","eval-js"],"css":["prismjs/themes/prism.css"],"js":["prismjs/prism.js","prismjs/components.js","prismjs/components/prism-go.min.js","prismjs/components/prism-yaml.min.js","prismjs/components/prism-bash.min.js","prismjs/components/prism-shell-session.min.js"]},"github":{"url":"https://github.com/whuanle/kubernetes"},"livereload":{},"splitter":{},"search":{},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":false},"alerts":{},"advanced-emoji":{"embedEmojis":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"insert-logo":{"style":"background: none; max-height: 50px; min-height: 50px","url":"/images/logo.jpg"}},"theme":"default","author":"痴者工良","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Kubernetes - 痴者工良","language":"zh-hans","links":{"sidebar":{"痴者工良的博客":"https://www.whuanle.cn"}},"gitbook":"*","description":"这是一本关于 Kubernetes 的书，作者 痴者工良"},"file":{"path":"3.pod/4.pod_network.md","mtime":"2021-11-16T23:18:37.541Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-11-16T23:21:22.294Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

